package com.linkhub.oms.component.aliyunoss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.linkhub.oms.component.aliyunoss.config.properties.AliyunOssProperties;
import com.linkhub.oms.component.aliyunoss.dto.PutObjectResultDTO;
import com.linkhub.oms.component.aliyunoss.service.OSSApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;

/**
 * @Description: OSSApiService
 * @Author: echo
 * @Date: 2024/5/9 20:28
 * @Version: 1.0
 */
@Slf4j
@Service
public class OSSApiServiceImpl implements OSSApiService {

    @Resource
    private OSS ossClient;

    @Resource
    private AliyunOssProperties ossProperties;

    @Override
    public PutObjectResultDTO putObject(String objectName, InputStream inputStream) {
        String bucketName = ossProperties.getBucketName();
        log.info("开始上传文件... ... objectName:{}, bucketName:{}", objectName, bucketName);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
        ossClient.putObject(putObjectRequest);
        log.info("成功上传文件... ... objectName:{}, bucketName:{}", objectName, bucketName);
        return PutObjectResultDTO.create(objectName, ossProperties.acquireEndpoint());
    }
}
